package com.yd.sdk;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.VibrationEffect;
import android.os.Vibrator;

import com.google.android.play.core.review.ReviewInfo;
import com.google.android.play.core.review.ReviewManager;
import com.google.android.play.core.review.ReviewManagerFactory;
import com.google.android.play.core.review.model.ReviewErrorCode;
import com.google.android.play.core.tasks.Task;
import com.unity3d.player.UnityPlayer;
import com.yd.sdk.IUnityActivityLifecycleEvents;

public class CommonUtils implements IUnityActivityLifecycleEvents {

    private static Vibrator _vibrator;
    private static Activity _activity;
    private static Handler _handler;
    private static ReviewManager _manager;
    @Override
    public void onCreate(Activity activity, Bundle savedInstanceState) {
        _activity = activity;
        _handler = new Handler();
        _vibrator = (Vibrator)activity.getSystemService(activity.VIBRATOR_SERVICE);
        _manager = ReviewManagerFactory.create(_activity);
    }

    @Override
    public void onPause() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantRes) {

    }

    public static void Vibrator(long millisenconds)
    {
        _handler.post(new Runnable() {
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    _vibrator. vibrate(VibrationEffect.createOneShot(20,150));
                }else
                {
                    _vibrator. vibrate(new long[] { 10, 10, }, -1);
                }

//                if(_vibrator.hasVibrator())
//                {
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//                        _vibrator. vibrate(VibrationEffect.createOneShot(20,150));
//                    }else
//                    {
//                        _vibrator. vibrate(new long[] { 10, 10, }, -1);
//                    }
//                   // _vibrator.vi
//                  //  _vibrator.vibrate(millisenconds); //参数标识  震动持续多少毫秒
//                }
            }
        });
    }

    public static void Comment()
    {
        _handler.post(new Runnable() {
            @Override
            public void run() {
                ReviewManager manager = ReviewManagerFactory.create(_activity);
                Task<ReviewInfo> request = manager.requestReviewFlow();
                request.addOnCompleteListener(task -> {
                    if (task.isSuccessful()) {
                        // We can get the ReviewInfo object
                        ReviewInfo reviewInfo = task.getResult();

                        Task<Void> flow = manager.launchReviewFlow(_activity, reviewInfo);
                        flow.addOnCompleteListener(task1 -> {
                            UnityPlayer.UnitySendMessage("SDKManagers","HandleCommentSucess","");
                            // The flow has finished. The API does not indicate whether the user
                            // reviewed or not, or even whether the review dialog was shown. Thus, no
                            // matter the result, we continue our app flow.
                        });

                        //UnityPlayer.UnitySendMessage("SDKManagers","HandleCommentSucess","");
                    } else {
                        UnityPlayer.UnitySendMessage("SDKManagers","HandleCommentFailed","");
                        // There was some problem, log or handle the error code.
//                        @ReviewErrorCode int reviewErrorCode = ((TaskException) task.getException()).getErrorCode();
                    }
                });
            }
        });
    }

}
